package com.itbour.service.material.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.material.FontDao;
import com.itbour.dto.com.FontInfoDto;
import com.itbour.service.material.FontService;


/**
*
* copyright   itbour team 2015
* @author     xc
*
*/

public class FontServiceImpl implements FontService {

	private FontDao fontDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = fontDao.selectFontCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<FontInfoDto> data = fontDao.selectFontInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<FontInfoDto> selectFontInfo(Map<String,Object> param) {
		return fontDao.selectFontInfo(param);
	}
	
	public void deleteFontInfo(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		fontDao.deleteFontInfoLogic(param);
	}

	public void addFont(FontInfoDto fontInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("file_name", fontInfo.getFile_name());// 检索同(文件)名字体
//		List<FontInfoDto> lstFont = fontDao.selectFontInfo(param);
//		if (lstFont != null && lstFont.size() > 0) {
//			// UPDATE
//			param.put("user_id", fontInfo.getUser_id());
//			param.put("url", fontInfo.getUrl());
//			param.put("font_name", fontInfo.getFont_name());
//			param.put("thumb_s", fontInfo.getThumb_s());
//			param.put("update_by", fontInfo.getUpdate_by());
//			param.put("font_id", lstFont.get(0).getFont_id());
//			fontDao.updateFontInfoById(param);
//		} else {
			// INSERT
			param.put("user_id", fontInfo.getUser_id());
			param.put("url", fontInfo.getUrl());
			param.put("font_name", fontInfo.getFont_name());
			param.put("thumb_s", fontInfo.getThumb_s());
			param.put("update_by", fontInfo.getUpdate_by());
			fontDao.insertFontInfo(param);
//		}
	}
	
	public void updFont(FontInfoDto fontInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("font_id", fontInfo.getFont_id());
		param.put("update_by", fontInfo.getUpdate_by());
		
		if (fontInfo.getUrl() != null) {
			param.put("url", fontInfo.getUrl());
		}
		if (fontInfo.getFont_name() != null) {
			param.put("font_name", fontInfo.getFont_name());
		}
		if (fontInfo.getFile_name() != null) {
			param.put("file_name", fontInfo.getFile_name());
		}
		if (fontInfo.getPublish() != null) {
			param.put("publish", fontInfo.getPublish());
		}
		if (fontInfo.getThumb_s() != null) {
			param.put("thumb_s", fontInfo.getThumb_s());
		}
		
		fontDao.updateFontInfoById(param);
	}
	
	public int chkFontName(Map<String,Object> param){
		return fontDao.chkFontName(param);
	}

	public FontDao getFontDao() {
		return fontDao;
	}

	public void setFontDao(FontDao fontDao) {
		this.fontDao = fontDao;
	}

}
